<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Api\CommonApi;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Model\User;
use session;

class PersonalController extends Controller
{

	 //加载个人信息页面以及数据
    public function personalDataView(Request $request)
    {
    	$users = $this->personalData($request);
    	// return view('Home/personal', ['userdata' => $users]);
    	if ($users) {
    		return view('Home/personal', ['userdata' => $users]);
    	} else {
    		return view('Home/personal');
    	}
    	
    }

    //负责查询个人用户信息 可共用使用
    public function personalData(Request $request) 
    {
    	if ($request->session()->has( 'userid' )) {
            // echo 11;
            $userid = $request->session()->get( 'userid' );
            $user = User::select(['id', 'user_name', 'name', 'password', 'phone', 'sex', 'email'])->where('id',  $userid)->get(); 
            return $user;
            // dd($user);       

        } else {
            // echo 22;exit;
            // return view('Home/personal');
            return view('/');
            
        }
    	
    }


    //加载修改用户相关信息
    public function personalDataEdit(Request $request) 
    {
    	$users = $this->personalData($request);

    	return view('Home/personal-edit',  ['userdata' => $users]);
    }


 
     /**
     * 修改用户信息
     * @author chenxiaodong <229168653@qq.com>
     * @param $request  Request的对象
     * @return  成功返回的数据是加载显示个人信息页面，失败则返回当前页面
     */
    public function personalDataUpdata(Request $request) 
    {

    	$user = User::select(['id', 'user_name'])->where('user_name', $_POST['uname'])->first();
    	
        $phonecheck = User::select(['id','phone'])->where('phone', $_POST['phone'])->first();
        // var_dump($_POST);exit;
        if ($user) {
        	echo json_encode(['status'=>1, 'msg'=>'该用户名已被使用或你未进行修改！']);
        	return;
        }

        if ($phonecheck) {
        	echo json_encode(['status'=>2, 'msg'=>'该手机号已被使用或你未进行修改！']);
        	return;
        }

    	$data = [
    		'user_name' => $_POST['uname'],
    		'name' => $_POST['name'],
    		'phone' => $_POST['phone'],
    		'email' => $_POST['email'],
    		'sex' =>$_POST['sex']
    	];
    	// exit;
    	// var_dump($data);exit;
    	$user = DB::table('user')
    				->where('id', $_POST['id'])
    				->update($data);
    	// var_dump($user);
    	if ($user) {
    		// echo '修改成功';
    		$request->session()->put('username', $_POST['uname']);
    		echo json_encode(['status'=>200, 'msg'=>'修改成功']);

    	}else{
    		echo json_encode(['status'=>3, 'msg'=>'修改失败']);
    		// return $this->personalDataEdit($request);
    	}

    }


    //加载密码修改页面
    public function personalPasswordEdit()
    {
    	
    	return view('/Home/password-edit');
    }


    public function personalPasswordUpdata(Request $request)
    {
    	$users = $this->personalData($request);
        $currentpass = $request->input('currentpass');
        $updatapass = $request->input('updatapass');
        $confirmpass = $request->input('confirmpass');


        //判断当前页面是否输入正确
        if ( password_verify($currentpass, $users[0]->password)) {

            if (!preg_match('/^\S{6,20}$/', $updatapass)) {
                echo json_encode(['status'=>4, 'msg'=>'密码字符必须为6-20位，建议任意两种字符组合']);
                exit;
            }

            if ($updatapass==$confirmpass) {
                 $updatapass = password_hash($updatapass, PASSWORD_DEFAULT);

                //执行修改密码
                $bool =  DB::table('user')
                    ->where('id', $users[0]->id)
                    ->update(['password' => $updatapass]);

                if ($bool) {
                    //修改成功后删除session退出当前登录  返回登录页
                    $request->session()->forget('userid');
                    $request->session()->forget('username');
                    echo json_encode(['status'=>1, 'msg'=>'修改成功']); 
                    return;
                }

            } else {
                echo json_encode(['status'=>3, 'msg'=>'两次输入密码不一致！']); 
                return;
            }       

        } else {
            echo json_encode(['status'=>2, 'msg'=>'原来密码有误！']); 
            return;
        }
       
    	// return view('/Home/password-edit', ['userdata', $user]);
    }


}
